package com.bcgtgjyb.huanwen.customview.mylibrary.view58;

import com.bcgtgjyb.huanwen.customview.mylibrary.DataAnimatorValue;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * Created by bigwen on 2016/3/2.
 */
public class Loading58Path extends Component implements Component.DrawTask {
    private static final float SQRT3 = 1.7f;
    private static final float SQRT2 = 1.414f;
    private int ww = 0;
    private int hh = 0;
    private int two = 2;
    private int alpha = 255;
    private int three = 3;
    private int four = 4;
    private int fivehundred = 500;
    private Paint paint;
    private int runParam = -1;
    private DataAnimatorValue colorVA;
    private Path cirToSque = new Path();
    private Path triToCir = new Path();
    private Path squeToTri = new Path();
    private float values;
    private float param;
    private float squ;
    private Path pathSqure = new Path();

    /**
     * Loading58Path 构造方法
     *
     * @param context 内容
     */
    public Loading58Path(Context context) {
        super(context);
        init();
    }

    /**
     * Loading58Path 构造方法
     *
     * @param context 内容
     * @param attrs 属性
     */
    public Loading58Path(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        addDrawTask(this);
        paint = new Paint();
        paint.setColor(Color.RED);
        initColorChange();
    }

    /**
     * 开始
     *
     */
    public void start() {
        if (runParam == -1) {
            runParam = 1;
            invalidate();
        }
    }

    /**
     * 结束
     *
     */
    public void stop() {
        runParam = -1;
        clear();
    }

    /**
     * 下一个
     */
    public void nextPath() {
        runParam++;
        if (runParam == 5) {
            runParam = 1;
            triangle();
            clear();
        } else if (runParam == two) {
            circle();
        } else if (runParam == three) {
            square();
        } else {}
    }

    private void square() {
        float num = 0.4f * ww;
        AnimatorValue animatorValue = new AnimatorValue();
        animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue anivmatorValue, float v) {
                squ = (Math.abs(num - 0) * v);
                if (v == 1.0) {
                    animatorValue.stop();
                    runParam = four;
                }
                invalidate();
            }
        });
        animatorValue.start();
    }

    private void circle() {
        float num = 0.4f * ww;
        AnimatorValue animatorValue = new AnimatorValue();
        animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                param = (Math.abs(num - 0) * v);
                invalidate();
            }
        });
        animatorValue.start();
    }

    private void initWH(int width, int height) {
        this.ww = width;
        this.hh = height;
    }

    private void drawSquToTri(Canvas canvas) {
        squeToTri.reset();
        float value1 = (1 - SQRT3 / 2) * values;
        float value2 = values / 2;
        float x1 = value1;
        float y1 = hh - value2;
        float x2 = ww - value1;
        float y2 = hh - value2;
        float x3 = values;
        float y3 = 0;
        float x4 = ww - values;
        float y4 = 0;
        squeToTri.moveTo(x1, y1);
        squeToTri.lineTo(x2, y2);
        squeToTri.lineTo(x4, y4);
        squeToTri.lineTo(x3, y3);
        squeToTri.close();
        canvas.drawPath(squeToTri, paint);
    }

    private void drawTriToCir(Canvas canvas) {
        triToCir.reset();

        // 三角形 左下定点
        float x1 = (float) ((double)ww / two - SQRT3 / four * ww);

        // 三角形 右下顶点
        float x2 = (float) ((double)ww / two + SQRT3 / four * ww);

        // 三角形 顶部顶点
        float x3 = (float) (ww / 2d);
        float y3 = 0;

        // 下部 调节点
        float[] point3 = new float[2];
        point3[0] = (float) ((double)ww / 2);
        point3[1] = hh + param / SQRT3;

        // 左上 调节点
        float[] point1 = fx1((x3 + x1) / 2 - param);

        // 右上 调节点
        float[] point2 = fx2((x3 + x2) / 2 + param);
        float y2 = 0.75f * hh;
        float y1 = 0.75f * hh;
        triToCir.moveTo(x3, y3);
        triToCir.quadTo(point1[0], point1[1], x1, y1);
        triToCir.quadTo(point3[0], point3[1], x2, y2);
        triToCir.quadTo(point2[0], point2[1], x3, y3);
        triToCir.close();
        canvas.drawPath(triToCir, paint);
    }

    // 三角形一边的垂直平分线函数
    private float[] fx1(float pointX) {
        float[] point = new float[2];
        point[0] = pointX;
        point[1] = SQRT3 / 3 * pointX + (0.5f - SQRT3 / 6) * hh;
        return point;
    }

    // 三角形一边的垂直平分线函数
    private float[] fx2(float pointX) {
        float[] point = new float[2];
        point[0] = pointX;
        point[1] = -SQRT3 / 3 * pointX + (0.5f + SQRT3 / 6) * hh;
        return point;
    }

    private void drawCirToSque(Canvas canvas) {
        cirToSque.reset();
        float x1 = (float) ((double)ww / two);
        float y1 = 0;
        float x2 = ww;
        float y2 = (float) ((double)hh / two);
        float x3 = (float) ((double)ww / two);
        float y3 = hh;
        float x4 = 0;
        float y4 = (float) ((double)hh / two);
        float par = 0.8f;
        float xx1 = par * ww + squ;
        float[] point1 = fx4(xx1);
        float xx2 = par * ww + squ;
        float[] point2 = fx3(xx2);
        float xx3 = (1 - par) * ww - squ;
        float[] point3 = fx4(xx3);
        float xx4 = (1 - par) * ww - squ;
        float[] point4 = fx3(xx4);
        cirToSque.moveTo(x1, y1);
        cirToSque.quadTo(point1[0], point1[1], x2, y2);
        cirToSque.quadTo(point2[0], point2[1], x3, y3);
        cirToSque.quadTo(point3[0], point3[1], x4, y4);
        cirToSque.quadTo(point4[0], point4[1], x1, y1);
        cirToSque.close();
        canvas.drawPath(cirToSque, paint);
    }

    private float[] fx3(float x2) {
        float[] mParam = new float[two];
        mParam[0] = x2;
        mParam[1] = x2;
        return mParam;
    }

    // 调节点 函数
    private float[] fx4(float x1) {
        float[] mParam = new float[two];
        mParam[0] = x1;
        mParam[1] = ww - x1;
        return mParam;
    }

    /**
     * 清空
     */
    public void clear() {
    }

    /**
     * initColorChange 方法
     */
    private void initColorChange() {
        colorVA = new DataAnimatorValue();
        colorVA.setStart(0);
        colorVA.setEnd(alpha);
        colorVA.setDuration(fivehundred);
        colorVA.setCurveType(Animator.CurveType.LINEAR);
    }

    private void triangle() {
        AnimatorValue animatorValue = new AnimatorValue();
        int mOne = ww / two;
        animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                values = (Math.abs(mOne - 0) * v);
                invalidate();
            }
        });
        animatorValue.start();
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        initWH(getWidth(), getHeight());
        if (runParam == 1) {
            if (colorVA != null) {
                colorVA.start();
                int d2 = (int) colorVA.getAnimatedValue();
                paint.setColor(new Color(Color.argb(alpha, alpha - d2, d2, 0)));
            }
            drawSquToTri(canvas);
        }

        if (runParam == two) {
            if (colorVA != null) {
                colorVA.start();
                int dd = (int) colorVA.getAnimatedValue();
                paint.setColor(new Color(Color.argb(alpha, 0, alpha - dd, dd)));
            }
            drawTriToCir(canvas);
        }

        if (runParam == three) {
            if (colorVA != null) {
                colorVA.start();
                int d1 = (int) colorVA.getAnimatedValue();
                paint.setColor(new Color(Color.argb(alpha, d1, 0, alpha - d1)));
            }
            drawCirToSque(canvas);
        }
        if (runParam == four) {
            pathSqure.reset();
            pathSqure.moveTo(0, 0);
            pathSqure.lineTo(ww, 0);
            pathSqure.lineTo(ww, hh);
            pathSqure.lineTo(0, hh);
            pathSqure.close();
            canvas.drawPath(pathSqure,paint);
        }
    }
}
